#include "process.h"

#define TARGET_SIZE 1024*1024//想要下载的数据量即1字节
#define DSIZE 1024*10//假设每次的下载量

//void download()
//{
//    int target = TARGET_SIZE;
//    int total = 0;//已经下载的量
//  
//    while(total < target)
//    {
//        usleep(STIME);//用休眠时间模拟本轮下载时间
//        total += DSIZE;
//        process_v2(total*100/target);
//    }
//    printf("\n");
//}
//使用回调函数的版本
void download(callback_t cb)
{
    int testcnt = 100;
    int target = TARGET_SIZE;
    int total = 0;//已经下载的量
  
    while(total <= target)
    {
        usleep(STIME);
        total += DSIZE;
        double rate = total*100.0/target;
        if(rate > 50.0 && testcnt){
           total = target / 2;
           testcnt--;
        }
        cb(rate);//回调函数
    }
    cb(MAX_RATE);
    printf("\n");
}


int main()
{
    //二、
    //download();

    //三、
    download(process_v3);

    //一、
    //int rate = 0;
    //while(rate <= 100)
    //{
    //    process_v2(rate++);
    //    usleep(12345);
    //}
    //printf("\n");

    return 0;
}
